package mmi.gui.widget;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.Box;
import javax.swing.BoxLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import mmi.gui.MainFrame;
import mmi.gui.shape.Shape;

public class DrawingBar extends JToolBar {
	private static final long serialVersionUID = -8404801127058034168L;

	//	private MainFrame parent;

	JButton rectangleButton = initialiseDrawingBarButton("Rectangle", "rectangle.png");

	JButton ellipseButton = initialiseDrawingBarButton("Ellipse", "ellipse.png");
	JButton triangleButton = initialiseDrawingBarButton("Triangle", "polygon.png");
	JButton strlineButton = initialiseDrawingBarButton("Line", "line.png");
	JButton freeLineButton = initialiseDrawingBarButton("Free Line", "freeline.png");

	JButton addTextButton = initialiseDrawingBarButton("Text", "text.png");
	JButton moveObjectButton = initialiseDrawingBarButton("Move", "move.png");
	JButton topObjectButton = initialiseDrawingBarButton("Top", "top.png");
	JButton resizeObjectButton = initialiseDrawingBarButton("Resize", "scale.png");
	JButton rotateObjectButton = initialiseDrawingBarButton("Rotate", "rotate.png");
	JButton fillButton = initialiseDrawingBarButton("Fill", "fill.png");
	JButton deleteObjectButton = initialiseDrawingBarButton("Delete", "eraser.png");

        JSlider colourSlider = null;
        JPanel lineExample;
        JPanel line;
        JPanel p2;
        JPanel p3;

	public DrawingBar(final MainFrame parent) {
		//		this.parent = parent;

                JPanel p1 = new JPanel();
                p2 = new JPanel();
                p3 = new JPanel();
		GridLayout gl = new GridLayout(0,2);
		p1.setLayout(gl);
                p2.setLayout(gl);
                p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));

                this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));


		p1.add(rectangleButton);
		rectangleButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(java.awt.event.ActionEvent e)
			{
                                parent.getDrawingArea().setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
				parent.getDrawingArea().setShape(Shape.Shapes.Rectangle);
				parent.getDrawingArea().setDraw();
			}
		});

		p1.add(ellipseButton);
		ellipseButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(java.awt.event.ActionEvent e)
			{
                                parent.getDrawingArea().setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
				parent.getDrawingArea().setShape(Shape.Shapes.Ellipse);
				parent.getDrawingArea().setDraw();
			}
		});

		p1.add(triangleButton);
		triangleButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(java.awt.event.ActionEvent e)
			{
                                parent.getDrawingArea().setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
				parent.getDrawingArea().setShape(Shape.Shapes.Triangle);
				parent.getDrawingArea().setDraw();
			}
		});

		p1.add(strlineButton);
		strlineButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(java.awt.event.ActionEvent e)
			{
                                parent.getDrawingArea().setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
				parent.getDrawingArea().setShape(Shape.Shapes.Line);
				parent.getDrawingArea().setDraw();
			}
		});

                add(p1);

                lineExample = new JPanel();
                lineExample.setLayout(new BoxLayout(lineExample, BoxLayout.Y_AXIS));
                lineExample.setPreferredSize(new Dimension(50, 25));
                lineExample.setMaximumSize(new Dimension(50, 25));
                lineExample.setBackground(this.getBackground());

                line = new JPanel();
                line.setBackground(parent.getDrawingArea().getLineColour());
                line.setPreferredSize(new Dimension(50, parent.getDrawingArea().getLineWidth()-1));
                line.setMaximumSize(new Dimension(50, parent.getDrawingArea().getLineWidth()-1));
                lineExample.add(Box.createVerticalGlue());
                lineExample.add(line);

                p3.add(Box.createRigidArea(new Dimension(10,10)));
                p3.add(lineExample);
                p3.add(Box.createRigidArea(new Dimension(10,10)));

		colourSlider = new JSlider(2,50,2);
                colourSlider.setPreferredSize(new Dimension(10, 20));
                colourSlider.setMajorTickSpacing(10);
                colourSlider.setPaintTicks(true);
                colourSlider.setToolTipText("Line Width");
                colourSlider.addChangeListener(new ChangeListener() {
                    public void stateChanged(ChangeEvent event) {
                        parent.getDrawingArea().setLineWidth(colourSlider.getValue());
                        line.setPreferredSize(new Dimension(50, parent.getDrawingArea().getLineWidth()/2));
                        line.setMaximumSize(new Dimension(50, parent.getDrawingArea().getLineWidth()/2));
                        lineExample.remove(line);
                        lineExample.add(line);
                        validate();
                    }
                });
                p3.add(colourSlider);
                p3.add(Box.createRigidArea(new Dimension(10,10)));
                add(p3);

		p2.add(freeLineButton);
		freeLineButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(java.awt.event.ActionEvent e)
			{
                                Image img = Toolkit.getDefaultToolkit().getImage("img/cs_freeline.png");
                                Cursor cs = Toolkit.getDefaultToolkit().createCustomCursor( img, new Point(5,20), "" );
                                parent.getDrawingArea().setCursor(cs);
                                parent.getDrawingArea().setShape(Shape.Shapes.FreeLine);
				parent.getDrawingArea().setDraw();
			}
		});

		p2.add(addTextButton);
		addTextButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(java.awt.event.ActionEvent e)
			{
                                parent.getDrawingArea().setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
				parent.getDrawingArea().setText();
			}
		});

		p2.add(topObjectButton);
		topObjectButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(java.awt.event.ActionEvent e)
			{
                                Image img = Toolkit.getDefaultToolkit().getImage("img/cs_top.png");
                                Cursor cs = Toolkit.getDefaultToolkit().createCustomCursor( img, new Point(16,16), "" );
                                parent.getDrawingArea().setCursor(cs);
				parent.getDrawingArea().setTop();
			}
		});

		p2.add(moveObjectButton);
		moveObjectButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(java.awt.event.ActionEvent e)
			{
                                parent.getDrawingArea().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
				parent.getDrawingArea().setMove();
			}
		});

		p2.add(resizeObjectButton);
		resizeObjectButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(java.awt.event.ActionEvent e)
			{
                                Image img = Toolkit.getDefaultToolkit().getImage("img/cs_resize.png");
                                Cursor cs = Toolkit.getDefaultToolkit().createCustomCursor( img, new Point(16,16), "" );
                                parent.getDrawingArea().setCursor(cs);
				parent.getDrawingArea().setResize();
			}
		});

		p2.add(rotateObjectButton);
		rotateObjectButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(java.awt.event.ActionEvent e)
			{
                                Image img = Toolkit.getDefaultToolkit().getImage("img/cs_rotate.png");
                                Cursor cs = Toolkit.getDefaultToolkit().createCustomCursor( img, new Point(16,16), "" );
                                parent.getDrawingArea().setCursor(cs);
				parent.getDrawingArea().setRotate();
			}
		});

		p2.add(fillButton);
		fillButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(java.awt.event.ActionEvent e)
			{
                                Image img = Toolkit.getDefaultToolkit().getImage("img/cs_fill.png");
                                Cursor cs = Toolkit.getDefaultToolkit().createCustomCursor( img, new Point(20,20), "" );
                                parent.getDrawingArea().setCursor(cs);
				parent.getDrawingArea().setFill();
			}
		});

		p2.add(deleteObjectButton);
		deleteObjectButton.addActionListener(new java.awt.event.ActionListener()
		{
			/*
			 * (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(java.awt.event.ActionEvent e)
			{
                                Image img = Toolkit.getDefaultToolkit().getImage("img/cs_delete.png");
                                Cursor cs = Toolkit.getDefaultToolkit().createCustomCursor( img, new Point(12,10), "" );
                                parent.getDrawingArea().setCursor(cs);
				parent.getDrawingArea().setDelete();
			}
		});

		setFloatable(false);


                add(p2);

	}

	private JButton initialiseDrawingBarButton(String text, String iconLocation)
	{
		JButton button = new JButton();
		button.setIcon(new ImageIcon("img/" + iconLocation));
		button.setToolTipText(text);
		button.setMargin(new Insets(0, 0, 0, 0));
		button.setVerticalTextPosition(BOTTOM);
		button.setHorizontalTextPosition(CENTER);
		//		button.setText(text);

		return button;
	}
}
